<?php
namespace app\base\controller;

use think\Request;
use think\Loader;
use think\Session;
use think\Config;
use think\Cache;
class Base
{

    public $error = [];


    public function __construct()
    {


        $this->Cstatic= \app\base\controller\Cstatic::instance();
        w_s("CONTROLLER",$this);
        $this->quest =  $quest = Request::instance();
        $this->agents_ids =[];


        //当前模块名称
        $this->sidRows = $rows = Cache::get(w_g("sid"));
        w_s("sidRow",$rows);

        if(isset($this->sidRows['admin_group_id'])){
            //后台操作模块
            if(isset($this->sidRows['agent_id'])&&$this->sidRows['agent_id']!=0){

               $agents = tree($this->sidRows['agent_id']);
                if(!empty($agents)){
                    $this->agents_ids =    array_column($agents,"id");
                }
            }
            /**
             *   管理员组
             */



            if($this->sidRows['admin_group_id']==1){
                //管理员
                $this->agents_ids = "all";//不限制操作
            }


            w_s("agents_ids",$this->agents_ids);
            $this->admin_user_id = $rows['id'];
        }
        $this->_modeule = ucfirst(strtolower($quest->module()));
        //当前控制器名称
        $this->_controller = strtolower($quest->controller());
        //当前操作名称
        $this->_action = strtolower($quest->action());

        //当前模型
        w_s('controllerinit',$this->_controller.$this->_action);

        $validate = Loader::validate($this->_modeule.'Validate', 'validate', false, $this->_modeule);
        //过滤验证规则
        $this->currAction = $this->_controller.$this->_action;
        $this->filterVailterparam = $this->filterVailter($validate,$quest->param());
        //场景验证
        if($validate->hasScene($this->currAction)){
            if(!$validate->scene($this->currAction)->check($this->filterVailterparam)){
                $this->error['msg'] = $validate->getError();
            }
            if(!empty($this->error)){
                exit(json_encode(failJson($this->error['msg'])->getData()));
                exit;
            }
            array_filter($this->filterVailterparam,function($val,$key) {
                $this->$key = $val;
                return $val;
            },ARRAY_FILTER_USE_BOTH);
        }
        if(Config::has($this->_controller.".".$this->_action)){
            $this->Lang = Config::get($this->_controller)[$this->_action];
        }else{
            $this->Lang =[];
        }
        $this->scan_name = w_g("scan_name");
        $this->RealData = delwithData($this);
    }

    /**
     * @param $validate
     * @param $param
     * @return array  规则过滤
     */
    protected function filterVailter($validate,$param){

            if(is_array($param)&&!empty($param)){
                    $keys = array_keys($param); //参数里的key
            }else{
                return [];
            }
            if(!is_object($validate)){
                return [];
            }
            $Reflection = new \ReflectionClass($validate);
            $paramDefault = $Reflection->getDefaultProperties();
            if(isset($paramDefault['scene'][$this->currAction])){
               $currActionParam = $paramDefault['scene'][$this->currAction];
                if(!isset($keys)||empty($keys)){
                            return [];
                }
                $param = array_filter($param,function($val,$key)use($currActionParam){
                                        return in_array($key,$currActionParam);
                },ARRAY_FILTER_USE_BOTH );
                return $param;
            }
            return [];
    }


    /**
     * @return mixed 获取当前模型
     */
    protected function getmodel(){
            return $this->model;
    }

    /**
     * @param string $key
     * @return mixed|string  输出配置信息
     */
    protected function ecLang($key=""){
        return  isset($this->Lang[$key])?$this->Lang[$key]:"";
    }



    public function __set($name, $value)
    {
        try {

            $this->$name = $value;
        } catch (\Exception $e) {      // 捕获异常
            //$this->BaseRequestRule($e);
            echo 3, $e;
            exit();
        }

    }

    public function __get($name)
    {
        try {
            return $this->$name;
        } catch (\Exception $e) {      // 捕获异常
            //$this->BaseRequestRule($e);
            echo 3, $e;
            exit();
        }

    }


   /* public function _empty()
    {

       $seachconfig =  Cache::get("seachconfig");
        $where = [];
        //时间的处理
        $res =   $this->model->AdminPageNoAgent($where);
        if(empty($res)){
            return failJson("暂无记录");
        }
        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }*/

    /**
     * @param $where
     * @param string $msg
     * @return \think\response\Json PcPage
     */
    protected function baseIndex($where,$order="a.id desc",$msg="暂无记录"){
        $where = array_merge($this->BaseWhere,$where);
        $res =   $this->model->PcPage($where,$order);
        if(empty($res))
        {
            return failJson($msg);
        }
        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }



}
